﻿using System;
using UnityEngine;

namespace UnityStandardAssets.CinematicEffects
{
	// Token: 0x02000472 RID: 1138
	[ExecuteInEditMode]
	[RequireComponent(typeof(Camera))]
	[AddComponentMenu("Image Effects/Cinematic/Bloom")]
	[ImageEffectAllowedInSceneView]
	public class Bloom : MonoBehaviour
	{
		// Token: 0x170001ED RID: 493
		// (get) Token: 0x0600180D RID: 6157 RVA: 0x00066543 File Offset: 0x00064943
		public Shader shader
		{
			get
			{
				if (this.m_Shader == null)
				{
					this.m_Shader = Shader.Find("Hidden/Image Effects/Cinematic/Bloom");
				}
				return this.m_Shader;
			}
		}

		// Token: 0x170001EE RID: 494
		// (get) Token: 0x0600180E RID: 6158 RVA: 0x0006656C File Offset: 0x0006496C
		public Material material
		{
			get
			{
				if (this.m_Material == null)
				{
					this.m_Material = ImageEffectHelper.CheckShaderAndCreateMaterial(this.shader);
				}
				return this.m_Material;
			}
		}

		// Token: 0x0600180F RID: 6159 RVA: 0x00066596 File Offset: 0x00064996
		private void OnEnable()
		{
			if (!ImageEffectHelper.IsSupported(this.shader, true, false, this))
			{
				base.enabled = false;
			}
		}

		// Token: 0x06001810 RID: 6160 RVA: 0x000665B2 File Offset: 0x000649B2
		private void OnDisable()
		{
			if (this.m_Material != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_Material);
			}
			this.m_Material = null;
		}

		// Token: 0x06001811 RID: 6161 RVA: 0x000665D8 File Offset: 0x000649D8
		private void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			bool isMobilePlatform = Application.isMobilePlatform;
			int num = source.width;
			int num2 = source.height;
			if (!this.settings.highQuality)
			{
				num /= 2;
				num2 /= 2;
			}
			RenderTextureFormat format = (!isMobilePlatform) ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;
			float num3 = Mathf.Log((float)num2, 2f) + this.settings.radius - 8f;
			int num4 = (int)num3;
			int num5 = Mathf.Clamp(num4, 1, 16);
			float thresholdLinear = this.settings.thresholdLinear;
			this.material.SetFloat("_Threshold", thresholdLinear);
			float num6 = thresholdLinear * this.settings.softKnee + 1E-05f;
			Vector3 v = new Vector3(thresholdLinear - num6, num6 * 2f, 0.25f / num6);
			this.material.SetVector("_Curve", v);
			bool flag = !this.settings.highQuality && this.settings.antiFlicker;
			this.material.SetFloat("_PrefilterOffs", (!flag) ? 0f : -0.5f);
			this.material.SetFloat("_SampleScale", 0.5f + num3 - (float)num4);
			this.material.SetFloat("_Intensity", Mathf.Max(0f, this.settings.intensity));
			RenderTexture temporary = RenderTexture.GetTemporary(num, num2, 0, format);
			Graphics.Blit(source, temporary, this.material, (!this.settings.antiFlicker) ? 0 : 1);
			RenderTexture renderTexture = temporary;
			for (int i = 0; i < num5; i++)
			{
				this.m_blurBuffer1[i] = RenderTexture.GetTemporary(renderTexture.width / 2, renderTexture.height / 2, 0, format);
				Graphics.Blit(renderTexture, this.m_blurBuffer1[i], this.material, (i != 0) ? 4 : ((!this.settings.antiFlicker) ? 2 : 3));
				renderTexture = this.m_blurBuffer1[i];
			}
			for (int j = num5 - 2; j >= 0; j--)
			{
				RenderTexture renderTexture2 = this.m_blurBuffer1[j];
				this.material.SetTexture("_BaseTex", renderTexture2);
				this.m_blurBuffer2[j] = RenderTexture.GetTemporary(renderTexture2.width, renderTexture2.height, 0, format);
				Graphics.Blit(renderTexture, this.m_blurBuffer2[j], this.material, (!this.settings.highQuality) ? 5 : 6);
				renderTexture = this.m_blurBuffer2[j];
			}
			this.material.SetTexture("_BaseTex", source);
			Graphics.Blit(renderTexture, destination, this.material, (!this.settings.highQuality) ? 7 : 8);
			for (int k = 0; k < 16; k++)
			{
				if (this.m_blurBuffer1[k] != null)
				{
					RenderTexture.ReleaseTemporary(this.m_blurBuffer1[k]);
				}
				if (this.m_blurBuffer2[k] != null)
				{
					RenderTexture.ReleaseTemporary(this.m_blurBuffer2[k]);
				}
				this.m_blurBuffer1[k] = null;
				this.m_blurBuffer2[k] = null;
			}
			RenderTexture.ReleaseTemporary(temporary);
		}

		// Token: 0x04000D61 RID: 3425
		[SerializeField]
		public Bloom.Settings settings = Bloom.Settings.defaultSettings;

		// Token: 0x04000D62 RID: 3426
		[SerializeField]
		[HideInInspector]
		private Shader m_Shader;

		// Token: 0x04000D63 RID: 3427
		private Material m_Material;

		// Token: 0x04000D64 RID: 3428
		private const int kMaxIterations = 16;

		// Token: 0x04000D65 RID: 3429
		private RenderTexture[] m_blurBuffer1 = new RenderTexture[16];

		// Token: 0x04000D66 RID: 3430
		private RenderTexture[] m_blurBuffer2 = new RenderTexture[16];

		// Token: 0x02000473 RID: 1139
		[Serializable]
		public struct Settings
		{
			// Token: 0x170001EF RID: 495
			// (get) Token: 0x06001813 RID: 6163 RVA: 0x00066933 File Offset: 0x00064D33
			// (set) Token: 0x06001812 RID: 6162 RVA: 0x0006692A File Offset: 0x00064D2A
			public float thresholdGamma
			{
				get
				{
					return Mathf.Max(0f, this.threshold);
				}
				set
				{
					this.threshold = value;
				}
			}

			// Token: 0x170001F0 RID: 496
			// (get) Token: 0x06001815 RID: 6165 RVA: 0x00066953 File Offset: 0x00064D53
			// (set) Token: 0x06001814 RID: 6164 RVA: 0x00066945 File Offset: 0x00064D45
			public float thresholdLinear
			{
				get
				{
					return Mathf.GammaToLinearSpace(this.thresholdGamma);
				}
				set
				{
					this.threshold = Mathf.LinearToGammaSpace(value);
				}
			}

			// Token: 0x170001F1 RID: 497
			// (get) Token: 0x06001816 RID: 6166 RVA: 0x00066960 File Offset: 0x00064D60
			public static Bloom.Settings defaultSettings
			{
				get
				{
					return new Bloom.Settings
					{
						threshold = 0.9f,
						softKnee = 0.5f,
						radius = 2f,
						intensity = 0.7f,
						highQuality = true,
						antiFlicker = false
					};
				}
			}

			// Token: 0x04000D67 RID: 3431
			[SerializeField]
			[Tooltip("Filters out pixels under this level of brightness.")]
			public float threshold;

			// Token: 0x04000D68 RID: 3432
			[SerializeField]
			[Range(0f, 1f)]
			[Tooltip("Makes transition between under/over-threshold gradual.")]
			public float softKnee;

			// Token: 0x04000D69 RID: 3433
			[SerializeField]
			[Range(1f, 7f)]
			[Tooltip("Changes extent of veiling effects in a screen resolution-independent fashion.")]
			public float radius;

			// Token: 0x04000D6A RID: 3434
			[SerializeField]
			[Tooltip("Blend factor of the result image.")]
			public float intensity;

			// Token: 0x04000D6B RID: 3435
			[SerializeField]
			[Tooltip("Controls filter quality and buffer resolution.")]
			public bool highQuality;

			// Token: 0x04000D6C RID: 3436
			[SerializeField]
			[Tooltip("Reduces flashing noise with an additional filter.")]
			public bool antiFlicker;
		}
	}
}
